<template>
  <header class="header">
    <h1>todos</h1>
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      v-model="isAll"
    >
    <label for="toggle-all"></label>
    <!-- 2.添加任务 -->
    <!-- 2.绑定设置按下事件,按回车添加任务 -->
    <!-- 2.3 v-model 绑定到新的字符串里 -->
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      v-model.trim="task"
      @keydown.enter="down"
      autofocus
    />
  </header>
</template>

<script>
export default {
  props: ['arr'],
  data () {
    return {
      task: ''
    }
  },
  methods: {
    // 2.5 使用this.$emit把值传给父组件中的list数组中
    down () {
      if (this.task === '') {
        alert('不能为空')
        return
      }
      this.$emit('arr', this.task)
      this.task = ''
    }
  },
  computed: {
    isAll: {
      set (checked) {
        this.arr.forEach(obj => obj.isDone = checked)
      },
      get () {
        return this.arr.length !== 0 && this.arr.every(obj => obj.isDone === true)
      }
    }
  }
}
</script>